package cn.luke.basic.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.luke.basic.entity.DtoPermission;
import cn.luke.basic.log_api.LogApi;
import cn.luke.basic.rs.ResponseResult;
import cn.luke.basic.services.ServicePermission;
import cn.luke.basic.services.ServiceRolePermission;
import cn.luke.basic.utils.MyAssert;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;

/**
 * Created by Luke on 2025/3/7 16:58.
 */
@RestController
@ResponseResult
@SaCheckLogin
@RequestMapping("/rolePermission")
public class ControllerRolePermission implements MyAssert {

    @Resource
    private ServicePermission servicePermission;
    @Resource
    private ServiceRolePermission serviceRolePermission;

    /**
     * 添加角色权限关联
     * @param params roleId,permissionIds
     */
    @LogApi("角色-管理权限")
    @SaCheckPermission("rolePermission.add")
    @PostMapping("/add")
    public void doAdd(@RequestBody Map<String, Object> params) {
        Long roleId = (long) idIsNull(params.get("roleId"), "角色ID不能为空");
        String permissionIds = strIsNull(params.get("permissionIds"), "权限IDs不能为空");
        serviceRolePermission.doAdd(roleId, permissionIds);
    }

    /**
     * 根据角色ID查询权限列表
     * @param params roleId
     */
    @SaCheckPermission("rolePermission.search")
    @PostMapping("/listByRole")
    public List<DtoPermission> listByRole(@RequestBody Map<String, Object> params) {
        Long roleId = (long) idIsNull(params.get("roleId"), "角色ID不能为空");
        return servicePermission.findByRoleId(roleId);
    }

}
